{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "06183ce9",
   "metadata": {},
   "source": [
    " 购物车程序项目：\n",
    "\n",
    "要求:\n",
    "\n",
    "    1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "    2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "    3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "    4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "417ad968",
   "metadata": {},
   "source": [
    "1. 用户输入余额 = input()\n",
    "\n",
    "2. 商品信息是一个列表 = [\n",
    "       [商品名称1,价格1],\n",
    "       [商品名称2,价格2],\n",
    "       [商品名称3,价格3],\n",
    "       [商品名称4,价格4],\n",
    "       ...\n",
    "    ]\n",
    "\n",
    "3. 打印数据（print）\n",
    "\n",
    "\n",
    "4. 用户输入\n",
    "\n",
    "5. 用户输入的金额  与  选择商品的价格 对比\n",
    "    1. 钱够\n",
    "    2. 钱不够\n",
    "6. 可以随时退出 （用什么循环？）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c3ca0b34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['珍珠奶茶小杯', 6],\n",
       " ['椰果奶茶中杯', 7],\n",
       " ['芝士奶盖大杯', 8],\n",
       " ['芋圆波波奶茶', 14],\n",
       " ['杨枝甘露', 8],\n",
       " ['多肉葡萄', 12]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 商品列表\n",
    "goods_list = [\n",
    "    ['珍珠奶茶小杯',6],\n",
    "    ['椰果奶茶中杯',7],\n",
    "    ['芝士奶盖大杯',8],\n",
    "    ['芋圆波波奶茶',14],\n",
    "    ['杨枝甘露',8],\n",
    "    ['多肉葡萄',12]\n",
    "]\n",
    "goods_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0278f1f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'珍珠奶茶小杯'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# goods_list[0] 是不是还是一个列表？\n",
    "goods_list[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1c57185a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "goods_list[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6b148d1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的余额：20\n",
      "----------商品信息----------\n",
      "商品编号: 0 ,商品的名称: 珍珠奶茶小杯 ,商品价格: 6 \n",
      "商品编号: 1 ,商品的名称: 椰果奶茶中杯 ,商品价格: 7 \n",
      "商品编号: 2 ,商品的名称: 芝士奶盖大杯 ,商品价格: 8 \n",
      "商品编号: 3 ,商品的名称: 芋圆波波奶茶 ,商品价格: 14 \n",
      "商品编号: 4 ,商品的名称: 杨枝甘露 ,商品价格: 8 \n",
      "请输入正确的商品编号（输入‘q’退出购买）：3\n",
      "已经将 【芋圆波波奶茶】加入到您的购物车，您的余额还有 【6】\n",
      "----------商品信息----------\n",
      "商品编号: 0 ,商品的名称: 珍珠奶茶小杯 ,商品价格: 6 \n",
      "商品编号: 1 ,商品的名称: 椰果奶茶中杯 ,商品价格: 7 \n",
      "商品编号: 2 ,商品的名称: 芝士奶盖大杯 ,商品价格: 8 \n",
      "商品编号: 3 ,商品的名称: 芋圆波波奶茶 ,商品价格: 14 \n",
      "商品编号: 4 ,商品的名称: 杨枝甘露 ,商品价格: 8 \n",
      "请输入正确的商品编号（输入‘q’退出购买）：3\n",
      "您的购物车有【[['芋圆波波奶茶', 14]]】,您的余额还有【6】\n",
      "您的余额不足,请开始零元购\n"
     ]
    }
   ],
   "source": [
    "### 1. 商品列表\n",
    "goods_list = [\n",
    "    ['珍珠奶茶小杯',6],\n",
    "    ['椰果奶茶中杯',7],\n",
    "    ['芝士奶盖大杯',8],\n",
    "    ['芋圆波波奶茶',14],\n",
    "    ['杨枝甘露',8]\n",
    "]\n",
    "### 要添加用户的商品列表：\n",
    "shopping_cart=[]\n",
    "\n",
    "# 2. 用户输入\n",
    "Alipay = int(input('请输入你的余额：'))\n",
    "\n",
    "# 3. 打印结果 :  商品编号：xxx , 商品的名称:xxx，商品价格: xxx\n",
    "# goods_list[0][0] 商品名称 goods_list[0][1] 商品价格\n",
    "\n",
    "# 占位符 %s s-->str\n",
    "# print(\"商品编号: %s ,商品的名称: %s ,商品价格: %s \"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "# print(\"商品编号: %s ,商品的名称: %s ,商品价格: %s \"%(1,goods_list[1][0],goods_list[1][1]))\n",
    "# print(\"商品编号: %s ,商品的名称: %s ,商品价格: %s \"%(2,goods_list[2][0],goods_list[2][1]))\n",
    "# print(\"商品编号: %s ,商品的名称: %s ,商品价格: %s \"%(3,goods_list[3][0],goods_list[3][1]))\n",
    "# print(\"商品编号: %s ,商品的名称: %s ,商品价格: %s \"%(4,goods_list[4][0],goods_list[4][1]))\n",
    "while True:\n",
    "    print('----------商品信息----------')\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号: %s ,商品的名称: %s ,商品价格: %s \"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "    # 4. 让用户输入商品编号进行商品的购买\n",
    "    choice = input('请输入正确的商品编号（输入‘q’退出购买）：') # input()默认是 str\n",
    "    #    Alipay VS 用户选择的商品的价格\n",
    "    \n",
    "    if choice.isdigit():\n",
    "    \n",
    "        # 一、用户输入的是编号\n",
    "        # ①. 编号在列表范围内\n",
    "        if int(choice) >=0 and int(choice) < len(goods_list):\n",
    "            # ------------------------------------------------------------------------\n",
    "            # 1. 钱包够，扣钱，给Alipay赋值扣完之后的金额 2. 钱不够，提示用户\n",
    "            # goods_list[choice][1] ----> 用户选择的商品的 价格\n",
    "            # goods_list[choice] = ['椰果奶茶中杯', 7]\n",
    "            if Alipay >= goods_list[int(choice)][1]:\n",
    "                Alipay = Alipay - goods_list[int(choice)][1] #扣钱\n",
    "                shopping_cart.append(goods_list[int(choice)])\n",
    "                print(\"已经将 【%s】加入到您的购物车，您的余额还有 【%s】\"%(goods_list[int(choice)][0],Alipay))\n",
    "            else:\n",
    "                print(\"您的购物车有【%s】,您的余额还有【%s】\"%(shopping_cart,Alipay))\n",
    "                print(\"您的余额不足,请开始零元购\")\n",
    "                break\n",
    "        else:\n",
    "            # ②. 编号不在列表范围内\n",
    "            print(\"您选择的商品不存在，请输入正确的编号（输入‘q’退出购买）\")\n",
    "\n",
    "    \n",
    "    elif choice == 'q':\n",
    "        # 二、用户输入 ‘q’ 退出\n",
    "        print(\"您的购物车有【%s】,您的余额还有【%s】\"%(shopping_cart,Alipay))\n",
    "        break\n",
    "    \n",
    "    else:\n",
    "        # 三、用户输入什么都不是\n",
    "        print(\"您输入有问题，请重新输入！请输入正确的商品编号（输入‘q’退出购买）\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "39ae3e49",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：假如有1万件商品？怎么做？\n",
    "# 请 使用  for + range（） 实现上述 5行代码的结果 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3e263d10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 珍珠奶茶小杯 6\n",
      "1 椰果奶茶中杯 7\n",
      "2 芝士奶盖大杯 8\n",
      "3 芋圆波波奶茶 14\n",
      "4 杨枝甘露 8\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(i,goods_list[i][0],goods_list[i][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5a9a3dbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(goods_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3c9560a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "商品编号: 0 ,商品的名称: 珍珠奶茶小杯 ,商品价格: 6 \n",
      "商品编号: 1 ,商品的名称: 椰果奶茶中杯 ,商品价格: 7 \n",
      "商品编号: 2 ,商品的名称: 芝士奶盖大杯 ,商品价格: 8 \n",
      "商品编号: 3 ,商品的名称: 芋圆波波奶茶 ,商品价格: 14 \n",
      "商品编号: 4 ,商品的名称: 杨枝甘露 ,商品价格: 8 \n",
      "商品编号: 5 ,商品的名称: 多肉葡萄 ,商品价格: 12 \n"
     ]
    }
   ],
   "source": [
    "# 思考： 如果增加商品了，或者下架商品了  5 是不是变了？\n",
    "#        5 是什么？--> 商品个数 -----> 列表的长度决定的！\n",
    "for i in range(len(goods_list)):\n",
    "    print(\"商品编号: %s ,商品的名称: %s ,商品价格: %s \"%(i,goods_list[i][0],goods_list[i][1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c62705ce",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-501feb1b9094>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mgetpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mhide_card\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetpass\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"hide_card:\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mcount\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhide_card\u001b[0m \u001b[1;33m<\u001b[0m\u001b[1;36m100\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mhide_card\u001b[0m \u001b[1;33m>\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\python\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mgetpass\u001b[1;34m(self, prompt, stream)\u001b[0m\n\u001b[0;32m    841\u001b[0m             warnings.warn(\"The `stream` parameter of `getpass.getpass` will have no effect when using ipykernel\",\n\u001b[0;32m    842\u001b[0m                     UserWarning, stacklevel=2)\n\u001b[1;32m--> 843\u001b[1;33m         return self._input_request(prompt,\n\u001b[0m\u001b[0;32m    844\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    845\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\python\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m    902\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    903\u001b[0m                 \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 904\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    905\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    906\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"yes, you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think smaller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5b75bd17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module getpass:\n",
      "\n",
      "NAME\n",
      "    getpass - Utilities to get a password and/or the current user name.\n",
      "\n",
      "MODULE REFERENCE\n",
      "    https://docs.python.org/3.8/library/getpass\n",
      "    \n",
      "    The following documentation is automatically generated from the Python\n",
      "    source files.  It may be incomplete, incorrect or include features that\n",
      "    are considered implementation detail and may vary between Python\n",
      "    implementations.  When in doubt, consult the module reference at the\n",
      "    location listed above.\n",
      "\n",
      "DESCRIPTION\n",
      "    getpass(prompt[, stream]) - Prompt for a password, with echo turned off.\n",
      "    getuser() - Get the user name from the environment or password database.\n",
      "    \n",
      "    GetPassWarning - This UserWarning is issued when getpass() cannot prevent\n",
      "                     echoing of the password contents while reading.\n",
      "    \n",
      "    On Windows, the msvcrt module will be used.\n",
      "\n",
      "CLASSES\n",
      "    builtins.UserWarning(builtins.Warning)\n",
      "        GetPassWarning\n",
      "    \n",
      "    class GetPassWarning(builtins.UserWarning)\n",
      "     |  Base class for warnings generated by user code.\n",
      "     |  \n",
      "     |  Method resolution order:\n",
      "     |      GetPassWarning\n",
      "     |      builtins.UserWarning\n",
      "     |      builtins.Warning\n",
      "     |      builtins.Exception\n",
      "     |      builtins.BaseException\n",
      "     |      builtins.object\n",
      "     |  \n",
      "     |  Data descriptors defined here:\n",
      "     |  \n",
      "     |  __weakref__\n",
      "     |      list of weak references to the object (if defined)\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Methods inherited from builtins.UserWarning:\n",
      "     |  \n",
      "     |  __init__(self, /, *args, **kwargs)\n",
      "     |      Initialize self.  See help(type(self)) for accurate signature.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Static methods inherited from builtins.UserWarning:\n",
      "     |  \n",
      "     |  __new__(*args, **kwargs) from builtins.type\n",
      "     |      Create and return a new object.  See help(type) for accurate signature.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Methods inherited from builtins.BaseException:\n",
      "     |  \n",
      "     |  __delattr__(self, name, /)\n",
      "     |      Implement delattr(self, name).\n",
      "     |  \n",
      "     |  __getattribute__(self, name, /)\n",
      "     |      Return getattr(self, name).\n",
      "     |  \n",
      "     |  __reduce__(...)\n",
      "     |      Helper for pickle.\n",
      "     |  \n",
      "     |  __repr__(self, /)\n",
      "     |      Return repr(self).\n",
      "     |  \n",
      "     |  __setattr__(self, name, value, /)\n",
      "     |      Implement setattr(self, name, value).\n",
      "     |  \n",
      "     |  __setstate__(...)\n",
      "     |  \n",
      "     |  __str__(self, /)\n",
      "     |      Return str(self).\n",
      "     |  \n",
      "     |  with_traceback(...)\n",
      "     |      Exception.with_traceback(tb) --\n",
      "     |      set self.__traceback__ to tb and return self.\n",
      "     |  \n",
      "     |  ----------------------------------------------------------------------\n",
      "     |  Data descriptors inherited from builtins.BaseException:\n",
      "     |  \n",
      "     |  __cause__\n",
      "     |      exception cause\n",
      "     |  \n",
      "     |  __context__\n",
      "     |      exception context\n",
      "     |  \n",
      "     |  __dict__\n",
      "     |  \n",
      "     |  __suppress_context__\n",
      "     |  \n",
      "     |  __traceback__\n",
      "     |  \n",
      "     |  args\n",
      "\n",
      "FUNCTIONS\n",
      "    getpass(prompt='', stream=None) method of ipykernel.ipkernel.IPythonKernel instance\n",
      "        Forward getpass to frontends\n",
      "        \n",
      "        Raises\n",
      "        ------\n",
      "        StdinNotImplentedError if active frontend doesn't support stdin.\n",
      "    \n",
      "    getuser()\n",
      "        Get the username from the environment or password database.\n",
      "        \n",
      "        First try various environment variables, then the password\n",
      "        database.  This works on Windows as long as USERNAME is set.\n",
      "\n",
      "DATA\n",
      "    __all__ = ['getpass', 'getuser', 'GetPassWarning']\n",
      "\n",
      "FILE\n",
      "    d:\\python\\lib\\getpass.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(getpass)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e8db0f5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
